package com.abc.bookstore.application;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.abc.bookstore.CommandBookstore;
import com.abc.bookstore.IBookstore;
import com.abc.bookstore.argot.BookLoader;
import com.argot.TypeException;
import com.argot.TypeLibrary;
import com.argot.TypeLibraryLoader;
import com.argot.common.CommonLoader;
import com.argot.dictionary.DictionaryLoader;
import com.argot.meta.MetaLoader;
import com.argot.network.NetArgotLoader;
import com.argot.remote.RemoteLoader;
import com.colony.ColonyEnvironment;
import com.colony.ColonyException;
import com.colony.channel.ChannelLoader;
import com.colony.container.simple.SimpleContainer;
import com.colony.container.zone.ZoneLoader;
import com.colony.networkvm.NvmClient;
import com.colony.networkvm.NvmLoader;
import com.colony.remote.RemoteException;
import com.colony.remote.rpc.RpcLoader;
import com.colony.transport.socket.SocketClient;
import com.colony.zone.CRL;

/**
 * This is the client version of the bookstore.  It demonstrates how Colony is configured to
 * run as a client.  
 * 
 */
public class ClientBookstore
{
	private ColonyEnvironment _client;
	private TypeLibrary _library;
	private String _host;
	
	public ClientBookstore( String host ) throws Exception
	{
		_host = host;
		_library = configTypeLibrary();
		
		SimpleContainer clientContainer = new SimpleContainer( _library );
		_client = new ColonyEnvironment(clientContainer,_library);
		SocketClient pipeClient = new SocketClient(_host,500);		
		_client.addClient(_host, new NvmClient( pipeClient, null, null ));		
	}
	
	private TypeLibrary configTypeLibrary() 
	throws TypeException, IOException
	{
		TypeLibraryLoader libraryLoaders[] = {
				new MetaLoader(),
				new DictionaryLoader(),
				new CommonLoader(),
				new RemoteLoader(),
				new NetArgotLoader(),
				new RpcLoader(),
				new NvmLoader(),
				new ChannelLoader(),
				new ZoneLoader(),
				new BookLoader()
			};
		
		TypeLibrary library = new TypeLibrary( libraryLoaders );
		return library;	
	}


	
	public IBookstore getBookstore( CRL location ) 
	throws RemoteException, ColonyException, TypeException
	{
		return (IBookstore) _client.getFront(location);
	}
	
	public static void main( String[] args )
	{
		try
		{
			System.out.println("Colony bookstore example application - client");

			if (args.length != 1 )
			{
				System.out.println("\nUsage: ClientBookstore <crl location>");
				System.out.println("eg: ClientBookstore crl://localhost/bookstore");
				System.exit(-1);
			}
			
			CRL bookstoreLocation = new CRL( args[0]);
			ClientBookstore client = new ClientBookstore( bookstoreLocation.getHost());	
			IBookstore bookstore = client.getBookstore( new CRL(args[0]) );
		
			CommandBookstore command = new CommandBookstore( bookstore );
			InputStreamReader isr = new InputStreamReader( System.in );
			BufferedReader reader = new BufferedReader( isr );
			command.getCommand( System.out, reader );
		
			System.out.println("closing" );
			
		}
		catch (Throwable e)
		{
			System.out.println("main caught exception");
			e.printStackTrace();
		}
 		
	}
	
}
